package com.example.heimashop.view.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.heimashop.base.BaseFragment;
import com.example.heimashop.databinding.FragmentCategoryBinding;
import com.example.heimashop.model.Category;
import com.example.heimashop.utils.ToastUtils;
import com.example.heimashop.view.activity.ProductListActivity;
import com.example.heimashop.view.adapter.CategoryAdapter;
import com.example.heimashop.viewmodel.CategoryViewModel;

/**
 * 分类Fragment
 * 页面4: CategoryFragment - 商品分类页面
 */
public class CategoryFragment extends BaseFragment<FragmentCategoryBinding> {

    private CategoryViewModel viewModel;
    private CategoryAdapter categoryAdapter;
    private CategoryAdapter subCategoryAdapter;

    @Override
    protected FragmentCategoryBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentCategoryBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initView() {
        viewModel = new ViewModelProvider(this).get(CategoryViewModel.class);

        // 初始化主分类适配器
        categoryAdapter = new CategoryAdapter();
        binding.rvCategory.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.rvCategory.setAdapter(categoryAdapter);

        // 初始化子分类适配器
        subCategoryAdapter = new CategoryAdapter();
        binding.rvSubCategory.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.rvSubCategory.setAdapter(subCategoryAdapter);
    }

    @Override
    protected void initData() {
        // 观察数据
        viewModel.categoriesLiveData.observe(getViewLifecycleOwner(), categories -> {
            if (categories != null && !categories.isEmpty()) {
                categoryAdapter.setData(categories);
                // 默认选中第一个
                if (categories.get(0).getChildren() != null) {
                    subCategoryAdapter.setData(categories.get(0).getChildren());
                }
            }
        });

        viewModel.errorLiveData.observe(getViewLifecycleOwner(), error -> {
            if (error != null) {
                ToastUtils.showShort(requireContext(), error);
            }
        });

        // 加载分类数据
        viewModel.getCategories();
    }

    @Override
    protected void initListener() {
        // 主分类点击
        categoryAdapter.setOnItemClickListener((category, position) -> {
            if (category.getChildren() != null && !category.getChildren().isEmpty()) {
                subCategoryAdapter.setData(category.getChildren());
            }
        });

        // 子分类点击
        subCategoryAdapter.setOnItemClickListener((category, position) -> {
            Intent intent = new Intent(getContext(), ProductListActivity.class);
            intent.putExtra("categoryId", category.getCatId());
            intent.putExtra("categoryName", category.getCatName());
            startActivity(intent);
        });
    }
}
